package com.app.ui.pager.home;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.net.res.user.User;
import com.app.ui.activity.account.AboutActivity;
import com.app.ui.activity.account.LoginActivity;
import com.app.ui.activity.account.UserUpdateActivity;
import com.app.ui.activity.base.BaseActivity;
import com.app.ui.event.UserEvent;
import com.app.ui.pager.BaseViewPager;
import com.app.utiles.image.ImageLoadingUtile;
import com.app.utiles.other.APKInfo;
import com.app.utiles.other.ActivityUtile;
import com.app.utiles.other.ToastUtile;
import com.wxj.zx.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的
 * Created by Administrator on 2016/9/7.
 */
public class HomeMePager extends BaseViewPager {

    @Bind(R.id.pat_head_pic_iv)
    ImageView patHeadPicIv;
    @Bind(R.id.pat_nickname_tv)
    TextView patNicknameTv;
    @Bind(R.id.sys_version_tv)
    TextView sysVersionTv;

    @Bind(R.id.me_login_out)
    TextView meLoginOut;


    public HomeMePager(BaseActivity baseActivity) {
        super(baseActivity);
    }

    @Override
    protected View onViewCreated() {
        View view = LayoutInflater.from(baseActivity).inflate(R.layout.pager_home_me, null);
        ButterKnife.bind(this, view);
        sysVersionTv.setText("v " + APKInfo.getInstance().getVersionName());
        User user = baseApplication.getUser();
        patNicknameTv.setText(user.getUserNickName());
        ImageLoadingUtile.loadingCircle(baseActivity, user.userHeadUrl,
                R.mipmap.default_head_pat_man, patHeadPicIv);
        EventBus.getDefault().register(this);
        return view;
    }


    @OnClick({R.id.sys_version_rl, R.id.sys_about_tv, R.id.me_login_out, R.id.pat_head_pic_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sys_version_rl:
                //检查更新
                ToastUtile.showToast("已经是最新版本了");
                break;
            case R.id.sys_about_tv:
                //关于
                ActivityUtile.startActivityCommon(AboutActivity.class);
                break;
            case R.id.pat_head_pic_iv:
                //个人资料
                ActivityUtile.startActivityCommon(UserUpdateActivity.class);
                break;
            case R.id.me_login_out:
                //退出登录
                baseApplication.setUser(null);
                ActivityUtile.startActivityCommon(LoginActivity.class);
                baseActivity.finish();
                break;

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnBack(UserEvent userEvent) {
        if (!userEvent.toCompareTag(this)) {
            return;
        }
        User user = baseApplication.getUser();
        patNicknameTv.setText(user.getUserNickName());
        ImageLoadingUtile.loadingCircle(baseActivity, user.userHeadUrl,
                R.mipmap.default_head_pat_man, patHeadPicIv);
    }

    @Override
    public void onDestory() {
        EventBus.getDefault().unregister(this);
    }

}
